package com.qys.livesMall.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.model.dto.FinancePayDTO;
import com.qys.livesMall.finance.service.FinanceBusinessService;
import com.qys.livesMall.finance.util.BusinessNoUtil;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.product.entity.H2ProductGoodsinfo;
import com.qys.livesMall.product.service.H2ProductGoodsinfoService;
import com.qys.livesMall.shop.modal.dto.shop.AppPurchase;
import com.qys.livesMall.shop.modal.entity.ShopGoodsInfo;
import com.qys.livesMall.shop.service.H2ShopBalanceService;
import com.qys.livesMall.shop.service.ShopGoodsInfoService;
import com.qys.livesMall.shop.service.ShopPurchaseService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @packageName:
 * @className:ShopPurchaseServiceImpl
 * @Description:
 * @author:guomx
 * @date:2023/6/718:01
 */
@Service
public class ShopPurchaseServiceImpl implements ShopPurchaseService {
    @Autowired
    H2ShopBalanceService balanceService;
    @Autowired
    private ShopGoodsInfoService shopGoodsInfoService;

    @Resource
    H2ProductGoodsinfoService productGoodsinfoService;
    @Resource
    FinanceBusinessService financeBusinessService;
    @Override
    @Transactional(rollbackFor={Exception.class})
    public FinancePayDTO
    out(AppPurchase purchase,Long infoId,BigDecimal money, String description) {

        FinancePayDTO dto = new FinancePayDTO();
        dto.setUserId(SecurityUtils.getUserId());
        dto.setShopId(purchase.getShopId());
        dto.setBusinessType(AccountBusinessTypeEnum.PURCHASE);
        dto.setAmountIncurred(money);
        dto.setBusinessNo(BusinessNoUtil.get(AccountBusinessTypeEnum.PURCHASE));
        financeBusinessService.pay(dto);
        return dto;
    }

    @Override
    public FinancePayDTO in(Long shopId,String businessNo, BigDecimal money, String description) {
        FinancePayDTO dto = new FinancePayDTO();
        dto.setUserId(SecurityUtils.getUserId());
        dto.setShopId(shopId);
        dto.setBusinessType(AccountBusinessTypeEnum.REFUND);
        dto.setAmountIncurred(money);
        dto.setBusinessNo(businessNo);
        financeBusinessService.pay(dto);
        return dto;
    }

    @Override
    @Transactional(rollbackFor={Exception.class})
    public ShopGoodsInfo addInventory(AppPurchase purchase) {
        H2ProductGoodsinfo productGoods = productGoodsinfoService.getById(purchase.getGoodId());
        LambdaQueryWrapper<ShopGoodsInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShopGoodsInfo::getGoodsId,productGoods.getId());
        wrapper.eq(ShopGoodsInfo::getShopId,purchase.getShopId());
        ShopGoodsInfo old = shopGoodsInfoService.getOne(wrapper);
        ShopGoodsInfo shopGoodsInfo = new ShopGoodsInfo();
        if (ObjectUtils.isNotEmpty(old)){
            shopGoodsInfo.setId(old.getId());
        }
        shopGoodsInfo.setShopId(purchase.getShopId());
        shopGoodsInfo.setGoodsName(productGoods.getName());
        shopGoodsInfo.setGoodsDesc(productGoods.getIntro());
        shopGoodsInfo.setGoodsId(purchase.getGoodId());
        shopGoodsInfo.setImage(productGoods.getImage());
        shopGoodsInfo.setPrice(productGoods.getPrice());
        shopGoodsInfo.setOriginalPrice(productGoods.getPrice());
        if(!shopGoodsInfoService.saveOrUpdate(shopGoodsInfo)) throw new QysException(MessageUtils.message("create.shop.goods.error"));
        return shopGoodsInfo;

    }
}
